<template>
	<view class="container">
	
		<uni-section title="基础信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms >
					<uni-forms-item label="昵称" required>
						<uni-easyinput v-model="baseData.nickName" placeholder="请输入昵称" />
					</uni-forms-item>
					<uni-forms-item label="年龄" required>
						<uni-easyinput v-model="baseData.age" placeholder="请输入年龄" />
					</uni-forms-item>
					<uni-forms-item label="性别" required>
						<uni-data-checkbox v-model="baseData.sex" :localdata="sexs" />
					</uni-forms-item>
					<!-- <uni-forms-item label="兴趣爱好" required>
						<uni-data-checkbox v-model="baseFormData.hobby" multiple :localdata="hobbys" />
					</uni-forms-item> -->
					<uni-forms-item label="自我介绍">
						<uni-easyinput type="textarea" v-model="baseData.introduction" placeholder="请输入自我介绍" />
					</uni-forms-item>
					<uni-forms-item label="生日">
						<uni-datetime-picker type="date"  v-model="baseData.birthday"/>
					</uni-forms-item>
					<uni-forms-item label="选择头像">
					
						<uni-icons type="upload" size="30" @click="selectPortrait"></uni-icons>
					</uni-forms-item>
					<uni-forms-item>
					
						
						<image style="width: 200px; height: 200px; background-color: #eeeeee;" :src="imageUrl+baseData.portrait"></image>
					</uni-forms-item>
						
					<button type="primary" @click="submit()">提交</button>
				</uni-forms>
				
			</view>
		</uni-section>

	
	
		
		
	</view>
</template>
<script>
	import store from '@/store/index.js';//需要引入store
	import constant from '@/store/constant.js'
	const baseUrl=constant.baseUrl;
	const baseImageUrl=constant.baseImageUrl;
	
	
	import md5 from 'js-md5';
	export default {
		computed:{
			errorMessage(){
				return store.state.user.errorMessage
			}
			
		},
		onLoad(option) {
			
			this.$data.baseData=JSON.parse(uni.getStorageSync("userInfo"))
			
			
		
		},
		data() {
			return {
				imageUrl:baseImageUrl,
				// 基础表单数据
				baseData: {
					userId:'',
					nickName: '',
					age: '',
					introduction: '',
					sex: 2,
					birthday: '',
					pwd:'',
					portrait:"",
					
				},
				basePwd:{
					oldpwd:'',
					pwd:'',
					repwd:''
				},
				// 表单数据
				alignmentFormData: {
					name: '',
					age: '',
				},
				// 单选数据源
				sexs: [{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 2
				}, {
					text: '保密',
					value: 0
				}],
				
				// 分段器数据
				current: 0,
				items: ['左对齐', '顶部对齐'],
				// 校验表单数据
				valiFormData: {
					name: '',
					age: '',
					introduction: '',
				},
			
				// 自定义表单数据
				customFormData: {
					name: '',
					age: '',
					hobby: []
				},
			
			
			}
		},
	
		
		onReady() {
			
		},
		methods: {
			selectPortrait(){
				let self=this;
				uni.chooseImage({
					crop:{"width":200,"height":200},
					count: 1,
					success: (res) => {
						
						uni.uploadFile({
							url: baseUrl+'/api/upload/uploadFile', 
							filePath: res.tempFilePaths[0],
							name: 'file',
							// formData: {
							// 	'user': 'test'
							// },
							success: (result) => {
								
								let data =JSON.parse(result.data);
								console.log(data)
								if(data.code==0){
									console.log(data.result.url)
									self.$data.baseData.portrait=data.result.url;
								}
								
							}
						});
					}
				})
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			
			submit() {
				
				
				let data={
					"userId":this.$data.baseData.userId,
					"nickName":this.$data.baseData.nickName,
					"age":this.$data.baseData.age,
					"introduction":this.$data.baseData.introduction,
					"sex":this.$data.baseData.sex,
					"birthday":this.$data.baseData.birthday,
					"portrait":this.$data.baseData.portrait,
					
					
				}
				store.dispatch('editUserData',data).then(()=>{
					uni.setStorageSync('userInfo', JSON.stringify(this.$data.baseData));
					uni.showToast({
						icon: 'none',
						title: "信息保存成功" 
					})
					setTimeout(() => {
						console.log("12312312")
						uni.switchTab({
							url:"/pages/user/userInfoAd"
						})
					}, 1000);
					// uni.navigateTo({
					// 	url:"/pages/user/userInfo"
					// })
				});
			},
			
			logout(){
				uni.removeStorageSync('authInfo');
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
	}
</script>
<style lang="scss">

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>